// pages/manage/manage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onReachBottomLoading: false,
    refreshed: false, // 将本属性设置为true，收起下拉刷新，可多次设置为true（即便原来已经是true了）
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    screenHeight: app.globalData.screenHeight,
    msgNumber:{
      custMagerNoticeNums:0
    },
    custMagerTaskStatOverview:{
      addDealerActualNums: 0,
      addDealerTargetNums: 0,
      clearCarActualNums: 0,
      clearCarTargetNums: 0,
      publishCarActualNums: 0,
      publishCarTargetNums: 0,
      visitDealerActualNums: 0,
      visitDealerTargetNums: 0
    },
    noticeList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  pullDownRefresh(){
    this.lifeInit();
    let self = this;
    setTimeout(() => {
      self.setData({
        'refreshed': true
      })
    }, 1000);
  },
  // 真正的初始化页面。
  lifeInit(){
    // 加载一个方法。来装载Token以及其他参数。
    this.InitUserStatus();
    app.globalData.manageComponentLoadTime = new Date();
  },
  async InitUserStatus(){
    await app.checkToken();
    let accountStatus = app.accountStatus;
    // 回填权限列表。
    let userInfo = app.globalData.userInfo;
    if (undefined != userInfo.roleList && null != userInfo.roleList) {
      this.setData({
        roleList: userInfo.roleList
      });
    }
    this.setData({
      accountStatus: accountStatus,
      userInfo:userInfo
    });
    if(this.getTabBar()){
      this.getTabBar().setData({
        roleList:userInfo.roleList,
        accountStatus:accountStatus,
        userInfo:userInfo,
        remindList:userInfo.remindList
      })
      this.getTabBar().initRegisterComponent();
    }
    this.loadDealerInfo();
    this.loadMsgUnreadStat();
    this.custMagerTaskStatOverview();
    this.custMagerNoticeList();
  },
  loadMsgUnreadStat(){
    app.sHttp.msgUnreadStat().then(res =>{
      this.setData({
        'msgNumber': res.result
      });
      if(this.getTabBar()){
        this.getTabBar().setData({
          'msgNumber': res.result
        })
      }
    },err=>{
    })
  },  // 加载下车商的信息。
  loadDealerInfo(){
    if(Object.keys(app.globalData.dealerInfo).length === 0){
      app.sHttp.dealerInfo().then(res =>{
        if(res.result.city == '市辖区'){
          res.result.city = res.result.province;
        }
        app.globalData.dealerRegion = [res.result.province, res.result.city , res.result.district];
        app.globalData.dealerInfo = res.result;
        // 并且把地址填充到
        // app.globalData.defaultCity = res.result.city || '全国';
        this.setData({
          'dealerInfo': app.globalData.dealerInfo
        })
      },err=>{
      })
    }else{
      this.setData({
        'dealerInfo': app.globalData.dealerInfo
      })
    }
  }, 
  custMagerNoticeList(){
    app.sHttp.custMagerNoticeList({pageNum:1,pageSize:3,queryType:1}).then(res =>{
      this.setData({
        noticeList: res.result.noticeList
      })
    },err=>{
    })
  },
  // 加载管理信息
  custMagerTaskStatOverview(){
    app.sHttp.custMagerTaskStatOverview().then(res =>{
      this.setData({
        'custMagerTaskStatOverview': res.result
      });
    },err=>{
    })
  },
  jumpToClue(){
    wx.navigateTo({
      url: '/pages/manage/clue/clue',
    })
  },
  jumpToExpand(){
    wx.navigateTo({
      url: '/pages/manage/expand/expand',
    })
  },
  jumpToNotice(){
    wx.navigateTo({
      url: '/pages/manage/notice/notice',
    })
  },
  jumpToInfo(e){
    wx.navigateTo({
      url: '/pages/manage/notice/read/read?id='+e.currentTarget.dataset.id,
    })
  },
  jumpToCarManage(){
    wx.navigateTo({
      url: '/pages/manage/carManage/carManage',
    })
  },
  jumpToCarDealerManage(){
    wx.navigateTo({
      url: '/pages/manage/carDealerManage/carDealerManage',
    })
  },
  onShow() {
    // 是否需要请求
    if(null == app.globalData.manageComponentLoadTime || app.needReload(app.globalData.manageComponentLoadTime)){
      this.lifeInit();
    }
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
  }
})